function []=save_ROIs(adata_dir,ROIs,ROItrans,fnames,mouse_id,date_id,ExpGroup,template,act_map)

warning('off','MATLAB:load:variableNotFound')
% if ~strcmp(mouse_id(1:3),'GK_')
%     disp('mouse ID problem!')
%     return
% end
disp(['Now saving ' adata_dir mouse_id '\' date_id '\' mouse_id '-Adata-' fnames{1}(4:strfind(fnames{1},'_')-1)])
save([adata_dir mouse_id '\' date_id '\' mouse_id '-Adata-' fnames{1}(4:strfind(fnames{1},'_')-1)],'ROIs','ROItrans','template','act_map','fnames','-append');

if ExpGroup(1)~=str2num(fnames{1}(4:strfind(fnames{1},'_')-1))
    [main_adata_file,main_mouse_id,main_date_id]=find_adata_file(ExpGroup(1),adata_dir);
    main=load([adata_dir main_mouse_id '\' main_date_id '\' main_adata_file],'ROIs','ROItrans');
    nbr_main_ROIs=length(main.ROIs);
    ROIs_in_main_coord=ROIs;
    for ind=1:length(ROIs)
        ROIs_in_main_coord(ind).indices=ROIs_in_main_coord(ind).indices-ROItrans(1)-ROItrans(2)*size(template,1);
    end
    ROIs_in_main_coord=rotate_ROIs(ROIs_in_main_coord,-ROItrans(3),size(template));

    
    
    if length(ROIs_in_main_coord)<length(main.ROIs)
        disp('WARNING - ROIs were deleted!')
        
    elseif length(ROIs_in_main_coord)==length(main.ROIs)
        disp('No ROIs added - checking correspondence of ROIs')
        for ind=1:length(ROIs_in_main_coord)
            if strcmp(ROIs_in_main_coord(ind).type,'c')
                % require 70% overlap
                prc_ovlp=length(intersect(main.ROIs(ind).indices,ROIs_in_main_coord(ind).indices))/length(ROIs_in_main_coord(ind).indices);
                if 0.5<prc_ovlp & prc_ovlp<1.5
                else
                    disp(['Warning - ROIs nbr ' num2str(ind) ' does not meet min overlap criteria']);
                    %return
                end
            else
                prc_ovlp=length(intersect(main.ROIs(ind).indices,ROIs_in_main_coord(ind).indices))/length(ROIs_in_main_coord(ind).indices);
                if 0<prc_ovlp & prc_ovlp<Inf
                else
                    disp(['Warning - ROIs nbr ' num2str(ind) ' does not meet min overlap criteria']);
                    %return
                end
            end
        end
    else
        disp('New ROIs added - checking correspondence of old ROIs')
        for ind=1:length(main.ROIs)
            if strcmp(main.ROIs(ind).type,'c')
                % require 70% overlap
                prc_ovlp=length(intersect(main.ROIs(ind).indices,ROIs_in_main_coord(ind).indices))/length(ROIs_in_main_coord(ind).indices);
                if 0.5<prc_ovlp & prc_ovlp<1.5
                else
                    disp(['Warning - ROIs nbr ' num2str(ind) ' does not meet min overlap criteria']);
                    %return
                end
            else
                prc_ovlp=length(intersect(main.ROIs(ind).indices,ROIs_in_main_coord(ind).indices))/length(ROIs_in_main_coord(ind).indices);
                if 0<prc_ovlp & prc_ovlp<Inf
                else
                    disp(['Warning - ROIs nbr ' num2str(ind) ' does not meet min overlap criteria']);
                    %return
                end
            end
        end
        go_on=input(['Do you want to add these new ROIs to all other Exps of the same ExpGroup? ']);
        if go_on
            if  isfield(main.ROIs,'activity')
                main.ROIs=rmfield(main.ROIs,'activity');
            end
                
            for ind=length(main.ROIs)+1:length(ROIs_in_main_coord)
                main.ROIs(ind)=ROIs_in_main_coord(ind);
            end
            ROIs=main.ROIs;
            disp(['Now saving ' adata_dir main_mouse_id '\' main_date_id '\' main_mouse_id '-Adata-' num2str(ExpGroup(1))])
            save([adata_dir main_mouse_id '\' main_date_id '\' main_mouse_id '-Adata-' num2str(ExpGroup(1))],'ROIs','-append');
            other_expIDs=setdiff(ExpGroup(2:end),str2num(fnames{1}(4:strfind(fnames{1},'_')-1)));
            for knd=other_expIDs'
                [curr_adata_file,curr_mouse_id,curr_date_id]=find_adata_file(knd,adata_dir);
                if ~isempty(curr_adata_file)
                    curr=load([adata_dir curr_mouse_id '\' curr_date_id '\' curr_adata_file],'ROIs','ROItrans');
                    if length(curr.ROIs)==nbr_main_ROIs
                        ROIs_in_curr_coord=rotate_ROIs(ROIs_in_main_coord,curr.ROItrans(3),size(template));
                        for ind=1:length(ROIs_in_curr_coord)
                            ROIs_in_curr_coord(ind).indices=ROIs_in_curr_coord(ind).indices+curr.ROItrans(1)+curr.ROItrans(2)*size(template,1);
                        end
                        for ind=length(curr.ROIs)+1:length(ROIs_in_curr_coord)
                            curr.ROIs(ind)=ROIs_in_curr_coord(ind);
                        end
                        ROIs=curr.ROIs;
                        disp(['Now saving ' adata_dir curr_mouse_id '\' curr_date_id '\' curr_mouse_id '-Adata-' num2str(knd)])
                        save([adata_dir curr_mouse_id '\' curr_date_id '\' curr_mouse_id '-Adata-' num2str(knd)],'ROIs','fnames','-append');
                    else
                        disp(['Exp ' num2str(knd) ' has probably not been analyzed yet']);
                    end
                end
            end
        end
    end
end
disp('--- Done saving ROIs ---');